local skel = fk.CreateSkill {
  name = "emo__fuchao",
  tags = {Skill.Compulsory},
  derived_piles = {"emo__fuchao"},
}

Fk:loadTranslationTable{
  ["emo__fuchao"] = "覆潮",
  [":emo__fuchao"] = "锁定技，当你使用牌指定唯一目标后，令其选择一项（与上次选择不同）：①将X张牌移出游戏；②将手牌补至X张；③失去1点体力，将X张手牌与移出牌交换（X为你已损失体力数）。",

  ["@emo__fuchao"] = "覆潮上次:",
  ["#emo__fuchao-ask"] = "覆潮:你可以令 %src 选择并执行一项",
  ["#emo__fuchao-choice"] = "覆潮:请选择一项",
  ["#emo__fuchao-remove"] = "覆潮:请将 %arg 张牌移出游戏",
  ["emo__fuchao_remove"] = "将%arg张牌移出游戏",
  ["emo__fuchao_draw"] = "将手牌补至%arg张",
  ["emo__fuchao_losehp"] = "失去1点体力，将%arg张手牌与移出牌交换",
  ["#emo__fuchao-exchange"] = "覆潮：选择%arg张手牌，与你移出的牌交换",

  ["$emo__fuchao1"] = "大丈夫行事，理应如火如荼！",
  ["$emo__fuchao2"] = "真男儿性情，当如烈火轰雷！",
}

skel:addEffect(fk.TargetSpecified, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data:isOnlyTarget(data.to) then
      return data.to and not data.to.dead
    end
  end,
  on_cost = function (self, event, target, player, data)
    event:setCostData(self, { tos = {data.to } })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = data.to
    if not to or to.dead then return end
    local x = player:getLostHp()
    local index = player:getMark("@emo__fuchao") -- 上次选择项
    local all_choices = {
      "emo__fuchao_remove:::" .. x,
      "emo__fuchao_draw:::" .. x,
      "emo__fuchao_losehp:::" .. (to ~= player and x or x + 1), -- 耦合
    }
    local choices = table.simpleClone(all_choices)
    local toCards = to:getCardIds("he")
    if x <= to:getHandcardNum() then
      table.remove(choices, 2)
    end
    if x == 0 or #toCards == 0 then -- 至少有1牌就能选1选项，不足扣全部
      table.remove(choices, 1)
    end
    if index > 0 then -- 删掉上次选择项
      table.removeOne(choices, all_choices[index])
    end
    if #choices == 0 then -- 没得选直接结束
      room:setPlayerMark(player, "@emo__fuchao", 0)
      return
    end
    local choice = room:askToChoice(to, {
      choices = choices, skill_name = skel.name, all_choices = all_choices,
      prompt = "#emo__fuchao-choice",
    })
    index = table.indexOf(all_choices, choice)
    room:setPlayerMark(player, "@emo__fuchao", index)
    if choice:startsWith("emo__fuchao_remove") then
      if #toCards > x then
        toCards = room:askToCards(to, {
          min_num = x, max_num = x, skill_name = skel.name, cancelable = false, include_equip = true,
          prompt = "#emo__fuchao-remove:::" .. x ,
        })
      end
      to:addToPile(skel.name, toCards, true, skel.name)
    elseif choice:startsWith("emo__fuchao_draw") then
      to:drawCards(x - to:getHandcardNum(), skel.name)
    else
      room:loseHp(to, 1, skel.name)
      if to.dead then return end
      x = player:getLostHp() -- 更新一下
      local moves = {}
      if #to:getPile(skel.name) > 0 then -- 获得的
        table.insert(moves, {
          from = to,
          to = to,
          ids = to:getPile(skel.name),
          toArea = Card.PlayerHand,
          moveReason = fk.ReasonExchange,
          skillName = skel.name,
          proposer = to,
        })
      end
      if x > 0 and not to:isKongcheng() then -- 移除的
        local put = room:askToCards(to, {
          min_num = x, max_num = x, skill_name = skel.name, cancelable = false, include_equip = false,
          prompt = "#emo__fuchao-exchange:::" .. x
        })
        table.insert(moves, {
          from = to,
          to = to,
          ids = put,
          toArea = Card.PlayerSpecial,
          specialName = skel.name,
          moveReason = fk.ReasonExchange,
          skillName = skel.name,
          proposer = to,
        })
      end
      if #moves > 0 then
        room:moveCards(table.unpack(moves))
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@emo__fuchao", 0)
end)

return skel
